#include "modbasic_dda.h"

ModbasicDda::ModbasicDda(Dda* pDda, Modbasic* pModbasic)
{
	m_pDda = pDda;
	m_pModbasic = pModbasic;
}

ModbasicDda::~ModbasicDda()
{
}

BOOL ModbasicDda::GetParam(UnitedParam* pDstParam, WORD id)
{
	BOOL readOk;
	const PmdMember* pmem = m_pDda->GetPmdMember(id);
	if (pmem == NULL) {
		return(FALSE);
	}

	switch (pDstParam->type) {
	case W_TYPE:
		readOk = m_pModbasic->ReadParam(&(pDstParam->data.w), id);
		break;
	case DW_TYPE:
		readOk = m_pModbasic->ReadLongParam(&(pDstParam->data.dv), id);
		break;
	case DWA_TYPE:
		readOk = m_pModbasic->ReadDvbArray(pDstParam->data.dva, pmem->size, id);
		break;
	case STR_TYPE:
		readOk = m_pModbasic->ReadString(pDstParam->data.str, pmem->size, id);
		break;
	default:
		readOk = FALSE;
	}
	return(readOk);
}

BOOL ModbasicDda::SetParam(WORD id, UnitedParam* pSrcParam)
{
	BOOL writeOk;
	switch (pSrcParam->type) {
	case W_TYPE:
		writeOk = m_pModbasic->WriteParam(id, pSrcParam->data.w);
		break;
	case DW_TYPE:
		writeOk = m_pModbasic->WriteLongParam(id, pSrcParam->data.dv);
		break;
	case DWA_TYPE:
		writeOk = m_pModbasic->WriteDvbArray(id, pSrcParam->data.dva);
		break;
	case STR_TYPE:
		writeOk = m_pModbasic->WriteString(id, pSrcParam->data.str);
		break;
	default:
		writeOk = FALSE;
		break;
	}
	return(writeOk);
}

BOOL ModbasicDda::LocateInstance(const char* formalTag)
{
	return(m_pDda->LocateInstance(formalTag));
}

BOOL ModbasicDda::GetParam(UnitedParam* pDstParam, const char* memName)
{
	WORD type = m_pDda->GetMemberType(memName);
	if (type == INVALID_TYPE) {
		return(FALSE);
	}

	pDstParam->type = type;
	WORD ddaId = m_pDda->GetDdaId(memName);
	return(GetParam(pDstParam, ddaId));
}

BOOL ModbasicDda::SetParam(const char* memName, UnitedParam* pSrcParam)
{
	WORD type = m_pDda->GetMemberType(memName);
	if (type == INVALID_TYPE) {
		return(FALSE);
	}

	pSrcParam->type = type;
	WORD ddaId = m_pDda->GetDdaId(memName);
	return(SetParam(ddaId, pSrcParam));
}
